package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.DeviceShadowDataVo;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;

/**
 * loT设备管理Controller
 *
 * @author zouguanghuan
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "loT设备管理相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询loT设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取loT设备管理列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出loT设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "loT设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出loT设备管理列表")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "loT设备管理数据");
    }

//    /**
//     * 获取loT设备管理详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
//    @GetMapping(value = "/{id}")
//    @ApiOperation("获取loT设备管理详细信息")
//    public R<Device> getInfo(@ApiParam(value = "loT设备管理ID", required = true)
//            @PathVariable("id") Long id) {
//        return R.ok(deviceService.selectDeviceById(id));
//    }

    /**
     * 新增IoT设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "IoT设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    @ApiOperation("新增IoT设备")
    public R add(@ApiParam(value = "IoT设备管理实体", required = true) @RequestBody DeviceDto dto) {
        deviceService.insertDevice(dto);
        return R.ok();
    }

    /**
     * 修改loT设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "loT设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改loT设备管理")
    public R edit(@ApiParam(value = "loT设备管理实体", required = true) @RequestBody DeviceDto dto) {
        deviceService.updateDevice(dto);
        return R.ok();
    }

    /**
     * 删除loT设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "loT设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{iotId}")
    @ApiOperation("删除loT设备管理")
    public R remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable String iotId) {
        deviceService.deleteDeviceById(iotId);
        return R.ok();
    }

    /**
     * 从华为云同步产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "IoT设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/syncProductList")
    @ApiOperation("同步Iot平台产品列表")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }


    /**
     * 查询所有产品信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品信息")
    public R<List<ProductVo>> getAllProducts() {
        return R.ok(deviceService.getAllProducts());
    }

    /**
     * 查询设备详细数据
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<DeviceDetailVo> getInfo(@PathVariable("iotId") String iotId) {
        return R.ok(deviceService.queryDeviceDetail(iotId));
    }

    /**
     * 查看设备上报的数据
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable("iotId") String iotId) {
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    /**
     * 查询产品详情(报警规则模块适用)
     */
    @ApiOperation("查询产品详情（报警规则模块使用)")
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProduct(@PathVariable("productKey") String productKey){
        return deviceService.queryProduct(productKey);
    }
}
